package com.youlai.boot.sport.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.sport.mapper.SportDataMapper;
import com.youlai.boot.sport.service.SportDataService;
import com.youlai.boot.sport.model.entity.SportData;
import com.youlai.boot.sport.model.form.SportDataForm;
import com.youlai.boot.sport.model.query.SportDataQuery;
import com.youlai.boot.sport.model.vo.SportDataVO;
import com.youlai.boot.sport.converter.SportDataConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 运动数据服务实现类
 *
 * @author cuisheng
 * @since 2025-03-31 12:41
 */
@Service
@RequiredArgsConstructor
public class SportDataServiceImpl extends ServiceImpl<SportDataMapper, SportData> implements SportDataService {

    private final SportDataConverter sportDataConverter;

    /**
    * 获取运动数据分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SportDataVO>} 运动数据分页列表
    */
    @Override
    public IPage<SportDataVO> getSportDataPage(SportDataQuery queryParams) {
        Page<SportDataVO> pageVO = this.baseMapper.getSportDataPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取运动数据表单数据
     *
     * @param id 运动数据ID
     * @return
     */
    @Override
    public SportDataForm getSportDataFormData(Long id) {
        SportData entity = this.getById(id);
        return sportDataConverter.toForm(entity);
    }
    
    /**
     * 新增运动数据
     *
     * @param formData 运动数据表单对象
     * @return
     */
    @Override
    public boolean saveSportData(SportDataForm formData) {
        SportData entity = sportDataConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新运动数据
     *
     * @param id   运动数据ID
     * @param formData 运动数据表单对象
     * @return
     */
    @Override
    public boolean updateSportData(Long id,SportDataForm formData) {
        SportData entity = sportDataConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除运动数据
     *
     * @param ids 运动数据ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteSportDatas(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的运动数据数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
